package com.feng.newconfig.config.feign;


import com.feng.common.enums.CommonErrorCode;
import com.feng.common.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @ClassName FunFeignFallback
 * @Description
 * @Author Tyler
 * @Date 2020/7/1 16:21
 **/
@Slf4j
@AllArgsConstructor
public class FunFeignFallback<T> implements MethodInterceptor {

    private final Class<T> targetType;
    private final String targetName;
    private final Throwable cause;

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        String errorMessage = cause.getMessage();
        log.error("FunFeignFallback: [{}.{}] serviceId: [{}] message:[{}]", targetType.getName(), method.getName(), targetName, errorMessage);
        throw new BizException(CommonErrorCode.APP_BIZ_ERROR);
    }

    @Override
    public int hashCode() {
        return Objects.hash(targetType);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        FunFeignFallback<?> that = (FunFeignFallback<?>) obj;
        return targetType.equals(that.targetType);
    }
}
